<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.leaderId" placeholder="教师编号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search">
        搜索
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      style="width: 100%; margin-top:10px;"
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="150" prop="id">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="赛事名称" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.subject.matchName }}
        </template>
      </el-table-column>
      <el-table-column label="组赛单位" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.subject.matchUnit }}</span>
        </template>
      </el-table-column>
      <el-table-column label="赛制" width="100" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.subject.matchRules === 0">
            个人赛
          </div>
          <div v-else>
            团队赛
          </div>
        </template>
      </el-table-column>
      <el-table-column label="负责人" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.leaderId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="获奖名次" width="150" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.prizeAward === 0">
            特等奖
          </div>
          <div v-if="scope.row.prizeAward === 1">
            一等奖
          </div>
          <div v-if="scope.row.prizeAward === 2">
            二等奖
          </div>
          <div v-if="scope.row.prizeAward === 3">
            三等奖
          </div>
          <div v-if="scope.row.prizeAward === 4">
            优秀奖
          </div>
          <div v-if="scope.row.prizeAward === 5">
            无
          </div>
        </template>
      </el-table-column>
      <el-table-column label="级别" width="150" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.level === 0">
            国家级
          </div>
          <div v-if="scope.row.level === 1">
            区级
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="150" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.conStatus === 0">
            未审核
          </div>
          <div v-if="scope.row.conStatus === 1">
            已结题
          </div>
          <div v-if="scope.row.conStatus === 2">
            不通过
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleView(scope.row)">
            详情
          </el-button>
          <el-button v-if="scope.row.conStatus === 0" type="primary" size="mini" @click="handleUpdate(scope.row)">
            审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <div>
        <span>项目详情</span>
        <table style="width:100%;" cellpadding="2" cellspacing="0" border="1" bordercolor="#000000">
          <tbody>
            <tr>
              <td>赛事名称</td>
              <td>{{ temp.subject.matchName }}</td>
            </tr>
            <tr>
              <td>组赛单位</td>
              <td>{{ temp.subject.matchUnit }}</td>
            </tr>
            <tr>
              <td>赛制</td>
              <td>{{ temp.subject.matchRules }}</td>
            </tr>
            <tr>
              <td>项目负责人</td>
              <td>{{ temp.leaderId }}</td>
            </tr>
            <tr>
              <td>联系电话</td>
              <td>{{ temp.subject.telephone }}</td>
            </tr>
            <tr>
              <td>电子邮箱</td>
              <td>{{ temp.subject.email }}</td>
            </tr>
            <tr>
              <td>获奖名次</td>
              <td>{{ temp.prizeAward }}</td>
            </tr>
            <tr>
              <td>级别</td>
              <td>{{ temp.level }}</td>
            </tr>
            <tr>
              <td>结题申请书</td>
              <td>{{ temp.conFile }}</td>
            </tr>
          </tbody>
        </table>
      </div>
      <div style="margin-top:30px">
        <span>预算资金</span>
        <table style="width:100%;" cellpadding="2" cellspacing="0" border="1" bordercolor="#000000">
          <tbody>
            <tr>
              <td>注册费</td>
              <td>{{ temp.free.registerFree }}</td>
            </tr>
            <tr>
              <td>差旅费</td>
              <td>{{ temp.free.businessFree }}</td>
            </tr>
            <tr>
              <td>培训费</td>
              <td>{{ temp.free.trainFree }}</td>
            </tr>
            <tr>
              <td>评审费</td>
              <td>{{ temp.free.reviewFree }}</td>
            </tr>
            <tr>
              <td>领队费</td>
              <td>{{ temp.free.leaderFree }}</td>
            </tr>
            <tr>
              <td>组织费</td>
              <td>{{ temp.free.organizeFree }}</td>
            </tr>
            <tr>
              <td>指导费</td>
              <td>{{ temp.free.guideFree }}</td>
            </tr>
            <tr>
              <td>材料费</td>
              <td>{{ temp.free.materialFree }}</td>
            </tr>
            <tr>
              <td>教师奖金</td>
              <td>{{ temp.free.teacherBonus }}</td>
            </tr>
            <tr>
              <td>合计费用</td>
              <td>{{ temp.free.sumFree }}</td>
            </tr>
          </tbody>
        </table>
      </div>
      <!-- 详情的表单 -->
      <div v-if="dialogStatus === 'view'" style="margin-top:30px">
        <el-form ref="dataForm" :model="temp" :rules="formRules" label-position="left" label-width="70px" style="width: 500px; margin-left: 50px;">
          <el-form-item label="意见" prop="opinion" label-width="120px">
            <el-input v-model="temp.opinion" type="textarea" disabled="true" />
          </el-form-item>
          <el-form-item label="审核状态">
            <el-radio-group v-model="temp.conStatus">
              <el-radio :label="1" disabled="true">通过</el-radio>
              <el-radio :label="2" disabled="true">不通过</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <!-- 审核的表单 -->
      <div v-if="dialogStatus === 'update'" style="margin-top:30px">
        <el-form ref="dataForm" :model="form" :rules="formRules" label-position="left" label-width="70px" style="width: 500px; margin-left: 50px;">
          <el-form-item label="意见" prop="opinion" label-width="120px">
            <el-input v-model="form.opinion" type="textarea" />
          </el-form-item>
          <el-form-item label="审核状态">
            <el-radio-group v-model="form.conStatus">
              <el-radio label="1">通过</el-radio>
              <el-radio label="2">不通过</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button v-if="dialogStatus === 'update'" type="primary" @click="review()">
          确定
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { adminList, getOne, review } from '@/api/conclusion'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  data() {
    return {
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        leaderId: undefined
      },
      list: null,
      total: 0,
      listLoading: true,
      textMap: {
        update: '详情',
        create: '添加'
      },
      dialogStatus: '',
      dialogFormVisible: false,
      temp: {
        subject: '',
        free: ''
      },
      form: {
        id: undefined,
        opinion: '',
        subStatus: undefined
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleFilter() {
      this.listQuery.pageNum = 1
      if (this.listQuery.title.length === 0) {
        this.listQuery.title = undefined
      }
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      adminList(this.listQuery).then(response => {
        this.list = response.data.content
        this.total = response.data.totalSize
        this.listLoading = false
      })
    },
    resetTemp() {
      this.temp = {}
    },
    resetForm() {
      this.form = {
        subId: '',
        opinion: '',
        subStatus: undefined
      }
    },
    handleView(row) {
      this.resetTemp()
      this.resetForm()
      getOne(row.id).then(response => {
        this.temp = response.data
      })
      this.form.id = row.id
      this.dialogStatus = 'view'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.resetTemp()
      this.resetForm()
      getOne(row.id).then(response => {
        this.temp = response.data
      })
      this.form.id = row.id
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    review() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          review(this.form).then(response => {
            const index = this.list.findIndex(v => v.id === this.form.id)
            this.list.splice(index, 1, response.data)
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: '审核成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>
